<template>
	<view class="maintenance-record">
		<scroll-view scroll-y="true">
			<view class="list">
				<view class="item" v-for="(item, index) in list" :key="index">
					<view class="title">
						<view class="top">
							<text class="status">已上报故障编号</text>
							<text class="date">{{item.created_at}}</text>
						</view>
						<text class="code">{{item.number}}</text>
						<view class="title-square"></view>
					</view>
					<view class="content">
						<view class="device-status">
							<view class="caption">设备故障状态</view>
							<view :class="['text', {'fault': item.status==1}, {'normal': item.status==2}]">{{status[item.status-1]}}</view>
						</view>
						<view class="desc-info">{{item.remark}}</view>
					</view>
				</view>
			</view>
			<view v-if="isEmpty" class="no-data">
				暂无数据
			</view>
			<view v-else class="no-more">
				<load-more :status="loadStatus"></load-more>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import LoadMore from '@/components/uni-load-more/uni-load-more'
	import {reportList} from '@/api/config'
	export default {
		data() {
			return {
				page: 1, // 页数
				limit: 0, // 默认十条
				requestKey: true, // 请求key
				loadStatus: 'more',
				isEmpty: false,
				status: ['故障未解除', '故障已解除'],
				list: []
			};
		},
		components: {
			LoadMore
		},
		onLoad() {
			this.requestRecord()
		},
		onPullDownRefresh() {
			this.init()
			this.requestRecord()
			setInterval(() => {
				uni.stopPullDownRefresh()
			}, 800)
		},
		onReachBottom() {
			if (!this.requestKey) return
			this.page++
			this.requestRecord()
		},
		methods: {
			// 初始化
			init() {
				this.page = 1
				this.isEmpty = false
				this.list = []
				this.requestKey = true
			},
			// 请求数据
			async requestRecord() {
				const params = {
					page: this.page
				}
				this.loadStatus = 'loading'
				const [err, res] = await reportList(params)
				if (res) {
					this.limit = res.data.per_page
					if (res.data.data.length==0 && this.page==1) {
						this.requestKey = false
						this.isEmpty = true
						return
					} else if (res.data.data.length==0 && this.page>1) {
						this.requestKey = false
						this.loadStatus = 'noMore'
						return
					}
					this.list = this.list.concat(res.data.data)
					if (res.data.data.length < 15) {
						this.requestKey = false 
						this.loadStatus = 'noMore'
					} else {
						this.loadStatus = 'more'
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	@import '@/static/scss/config.scss';
	@import '@/static/scss/mixin.scss';

	page {
		width: 100%;
		height: 100%;

		.maintenance-record {
			@include square(100%, linear-gradient(180deg, $bg-c, $bg-d));
			@include flex-col(flex-start);

			scroll-view {
				@include box($safe-width, 100%);

				.list {
					padding-top: 30rpx;

					.item {
						background-color: #fff;
						padding: 30rpx;
						border-radius: 4rpx;
						margin-bottom: 40rpx;

						.title {
							@include flex-col(flex-start, flex-start);
							padding-bottom: 30rpx;
							border-bottom: 2rpx solid $bg-b;

							.top {
								@include box-w(100%);
								@include flex-row(space-between);
								@include font-set($font-b, $color-c);
							}

							.code {
								@include font-set($font-d, $color-e);
							}

							.title-square {
								@include box(50rpx, 8rpx, $bg-c);
								align-self: flex-end;
								transform: skew(-30deg, 0deg);
							}
						}

						.content {
							padding-top: 30rpx;
							.device-status{
								@include box-w();
								@include flex-row(space-between);
								.caption{
									@include font-set($font-b, $color-c);
								}
								.text{
									&.fault{
										@include font-set($font-b, $color-k, 500);
									}
									&.normal{
										@include font-set($font-b, $color-f, 500);
									}
								}
							}
							.desc-info{
								margin-top: 20rpx;
								@include font-set($font-b, $color-d);
							}
						}
					}
				
				}
				.no-data{
					@include box-w();
					@include flex-row();
					color: $color-a;
				}
			}
		}
	}
</style>
